package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.PointMsg;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.*;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.log.LogInputStream;

import java.security.spec.DSAPrivateKeySpec;
import java.util.Date;
import java.util.List;

@Service
public class TravelCommendServiceImpl implements ITravelCommendService {
    @Autowired
    private TravelCommendMapper mapper;
    @Autowired
    private PointMsgMapper pointMsgMapper;
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private UserMapper userMapper;

    public PageInfo<TravelCommend> selectForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<TravelCommend> list = mapper.selectList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void saveOrUpdate(TravelCommend travelCommend) {
        PointMsg pointMsg = new PointMsg();
        pointMsg.setCreateTime(new Date());
        //根据游记id查出游记作者
        User author = travelMapper.getAuthorByTravelId(travelCommend.getTravelId());
        pointMsg.setUser(author);
        String msg = "";
        if (travelCommend.getType() == TravelCommend.TYPE_STRATEGY) {
//            msg = "您的游记" + "<<" + travelCommend.getTitle() + ">>" + "写得太好了,被设为推荐攻略,获得1000积分,这么多积分," +
//                    "<a href=\"/shopping/pointshop.html\">点我去积分商城逛逛吧</a>";
            msg="您的游记<u><a href=\"/travelContent.html?id="+travelCommend.getTravelId()+"\" style=\"color: #ff2169\"><<"+travelCommend.getTitle()+">>" +
                    "</a></u>写得太好了,被设为推荐攻略,获得1000积分,这么多积分,<u><a href=\"/shopping/pointshop.html\" style=\"color: #6A6AFF\" >点我去积分商城换个女朋友吧</a></u>";

            userMapper.addPoint(author.getId(),1000);
        } else if (travelCommend.getType() == TravelCommend.TYPE_MONTH) {
            msg="您的游记<u><a href=\"/travelContent.html?id="+travelCommend.getTravelId()+"\" style=\"color: #ff2169\"><<"+travelCommend.getTitle()+">>" +
                    "</a></u>写得太好了,被设为每月推荐,获得500积分,这么多积分,<u><a href=\"/shopping/pointshop.html\" style=\"color: #6A6AFF\" >点我去积分商城逛逛吧</a></u>";
            userMapper.addPoint(author.getId(),500);
        } else {
            msg="您的游记<u><a href=\"/travelContent.html?id="+travelCommend.getTravelId()+"\" style=\"color: #ff2169\"><<"+travelCommend.getTitle()+">>" +
                    "</a></u>写得太好了,被设为每周推荐,获得100积分,继续加油哦";
            userMapper.addPoint(author.getId(),100);

        }
        pointMsg.setMsg(msg);
        pointMsgMapper.insert(pointMsg);
        if (travelCommend.getId() == null) {
            if (travelCommend.getType() == TravelCommend.TYPE_STRATEGY) {
                mapper.deleteByState(TravelCommend.TYPE_STRATEGY);
            }
            mapper.insert(travelCommend);

        } else {
            Integer state = mapper.selectStateById(travelCommend.getId());
            if (travelCommend.getType() == TravelCommend.TYPE_STRATEGY
                    && state != TravelCommend.TYPE_STRATEGY) {
                mapper.deleteByState(TravelCommend.TYPE_STRATEGY);
            }
            mapper.updateByPrimaryKey(travelCommend);
        }

    }

    @Override
    public List<TravelCommend> selectCommendByType(TravelCommendQueryObject qo) {
        PageHelper.orderBy(qo.getOrderBy());
        Date date = new Date();
        qo.setEndTime(DateUtil.getEndDate(date));
        if (qo.getType() == TravelCommend.TYPE_WEEK) {
            qo.setBeginTime(DateUtil.getSevenDaysAgo(date));
        }
        if (qo.getType() == TravelCommend.TYPE_MONTH) {
            qo.setBeginTime(DateUtil.getAMonthAgo(date));
        }
        List<TravelCommend> list = mapper.selectList(qo);
        return list;
    }
}
